/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package htmlparser;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Helder
 */
public class ParserWeb2Local {

    public ParserWeb2Local() {
    }

    public String setHtmlFile(String filepath) throws IOException {
        String ressource = "";
        try {
            ressource = this.getContents(filepath);
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        }

       return (String) ressource;

    }

    private String getContents(String file) throws FileNotFoundException {
        BufferedReader reader = null;
        ArrayList<String> attributeList = new ArrayList<String>();
        try {
            reader = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        }

        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        String ls = System.getProperty("line.separator");
        int i = 1;
        try {
            while ((line = reader.readLine()) != null) {
                if ((line.contains("href") || line.contains("HREF") || line.contains("SRC")) || (line.contains("src") || line.contains("HREF") || line.contains("Src"))) {
                    attributeList.add(line);
                    line = changeLink(line);
                    stringBuilder.append(line);
                    stringBuilder.append(ls);
                } else {
                    stringBuilder.append(line);
                    stringBuilder.append(ls);
                }
            }

            System.out.println(attributeList.toString());
            return stringBuilder.toString();
        } catch (IOException e) {
            return e.getMessage();
        }

    }

    public String changeLink(String ressource) {
        String regex = getRegex(ressource);
        String sourceString = ressource;

        if (regex != "") {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(sourceString);
            StringBuffer sb = new StringBuffer();

            while (m.find()) {
                // For example: transform match to upper case
                String replacement = "";
                m.appendReplacement(sb, replacement);
            }

            m.appendTail(sb);

            sourceString = sb.toString();
        }
        return sourceString;
    }

    public String getRegex(String ressource) {
        //Estas variaveis terão que ser preenchidas com o dominio do site dinamicamentente
        String cursor[] = {"c:/"};
        String domain = "www.heu.pt/";
        StringBuilder regex = new StringBuilder();

        for (int i = 0; i < cursor.length; i++) {

            if (ressource.contains(cursor[i])) {
                regex.append(cursor[i]);
            }
        }
        if (ressource.contains(domain)) {
            regex.append(domain);
        }
        return regex.toString();
    }

//    public String decorateTheString(String orgStr) {
//        StringBuilder builder = new StringBuilder();
//        builder.append(orgStr);
//        builder.deleteCharAt(orgStr.length() - 1);
//        builder.insert(0, builder.hashCode());
//        return builder.toString();
//    }
    
//    public String parse(String ressource) throws FileNotFoundException, IOException {
//        BufferedReader reader = new BufferedReader(new FileReader(ressource));
//        String line = null;
//        StringBuilder stringBuilder = new StringBuilder();
//        String ls = System.getProperty("line.separator");
//
//        while ((line = reader.readLine()) != null) {
//            stringBuilder.append(line);
//            stringBuilder.append(ls);
//        }
//
//        return stringBuilder.toString();
//    }
}
